const mongoose = require('mongoose')

mongoose.connect('mongodb://localhost/playground')
    .then(() => console.log('数据库连接成功'))
    .catch(err => console.log(err, '数据库连接失败'))

//  创建集合规则
const userSchema = new mongoose.Schema({
    name: String,
    age: Number,
    email: String,
    password: String,
    hobbies: [String]
})

// 使用规则创建集合
const User = mongoose.model('User', userSchema)

// 查询用户集合中的所有文档 
// User集合对象对应mongoDB数据库中users集合
// User.find().then(result => console.log(result))

// User.find({_id: '5c09f267aeb04b22f8460968'}).then(result => console.log(result))

// User.findOne({name: '李四'}).then(result => console.log(result))
// User.find({age: {$gt: 20, $lt: 50}}).then(result => console.log(result))
// User.find({ hobbies: { $in: ['足球'] } }).then(result => console.log(result))
// User.find().select('-_id -hobbies -email -password').then(result => console.log(result))
//  根据年龄字段进行升序排列
// User.find().sort('age').select('-_id name age').then(result => console.log(result))
// 根据年龄字段进行降序排列 
// User.find().sort('-age').then(result => console.log(result))
User.find().skip(2).limit(2).then(result => console.log(result))